#!/bin/sh
# shellcheck shell=dash disable=SC2169 source=/dev/null
#
### BEGIN INIT INFO
# Provides:           snmpd snmptrapd
# Required-Start:     $network $local_fs
# Required-Stop:      $network $local_fs
# Default-Start:      2 3 4 5
# Default-Stop:       0 6
# Short-Description:  SNMP agents
# Description:        NET SNMP (Simple Network Management Protocol) Agents
### END INIT INFO
#
# Author:    Jochen Friedrich <jochen@scram.de>
#

export PATH=/sbin:/usr/sbin:/bin:/usr/bin

test -x /usr/sbin/snmpd || exit 0
test -x /usr/sbin/snmptrapd || exit 0

# Defaults
export MIBDIRS=/usr/share/snmp/mibs
SNMPDRUN=yes
SNMPDOPTS='-Lsd -Lf /dev/null -c /etc/snmp/snmpd-global.conf -I -smux,mteTrigger,mteTriggerConf -u snmp -g snmp -p /var/run/snmpd.pid'
TRAPDRUN=no
TRAPDOPTS='-Lsd -c /etc/snmp/snmptrapd-global.conf -p /var/run/snmptrapd.pid'

# Reads config file (will override defaults above)
[ -r /etc/default/snmpd ] && . /etc/default/snmpd

ssd_oknodo="-o"

# Cd to / before starting any daemons.
cd /

# Create compatibility link to old AgentX socket location
if [ "$SNMPDCOMPAT" = "yes" ]; then
  ln -sfn /var/agentx/master /var/run/agentx
fi

start() {
    # copy the SNMP config files in place in
    # case they don't exist yet
    if [[ ! -e /etc/config/snmp/snmpd-user.conf ]]; then
      cp -Rp /etc/config_templates/snmp /etc/config/
    fi

    if [ "$SNMPDRUN" = "yes" ] && [ -f /etc/config/snmp/snmpd-ccu3.conf ]; then
        printf "Starting SNMP daemon: "
        # shellcheck disable=SC2086
        if start-stop-daemon -q -S -x /usr/sbin/snmpd -- ${SNMPDOPTS}; then
          echo "OK"
        else
          echo "FAIL"
        fi
    fi

    if [ "$TRAPDRUN" = "yes" ] && [ -f /etc/config/snmp/snmptrapd.conf ]; then
        printf "Starting SNMP trap daemon: "
        # shellcheck disable=SC2086
        if start-stop-daemon -q -S -x /usr/sbin/snmptrapd -- ${TRAPDOPTS}; then
          echo "OK"
        else
          echo "FAIL"
        fi
    fi
}

stop() {
    if [ "$SNMPDRUN" = "yes" ] && [ -f /etc/config/snmp/snmpd-ccu3.conf ]; then
        printf "Stopping SNMP daemon: "
        if start-stop-daemon -q -K $ssd_oknodo -x /usr/sbin/snmpd; then
          echo "OK"
        else
          echo "FAIL"
        fi
    fi

    if [ "$TRAPDRUN" = "yes" ] && [ -f /etc/config/snmp/snmptrapd.conf ]; then
        printf "Stopping SNMP trap daemon: "
        if start-stop-daemon -q -K $ssd_oknodo -x /usr/sbin/snmptrapd; then
          echo "OK"
        else
          echo "FAIL"
        fi
    fi
}

reload() {
    if [ "$SNMPDRUN" = "yes" ] && [ -f /etc/config/snmp/snmpd-ccu3.conf ]; then
        printf "Reloading SNMP daemon: "
        if start-stop-daemon -q -K -s 1 -p /var/run/snmpd.pid -x /usr/sbin/snmpd; then
          echo "OK"
        else
          echo "FAIL"
        fi
    fi

    if [ "$TRAPDRUN" = "yes" ] && [ -f /etc/config/snmp/snmptrapd.conf ]; then
        printf "Reloading SNMP trap daemon: "
        if start-stop-daemon -q -K -s 1 -p /var/run/snmptrapd.pid -x /usr/sbin/snmptrapd; then
          echo "OK"
        else
          echo "FAIL"
        fi
    fi
}

case "$1" in
    start)
        start
        ;;

    stop)
        stop
        ;;

    restart)
        stop
        # Allow the daemons time to exit completely.
        sleep 2
        start
        ;;

    reload|force-reload)
        reload
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|force-reload}"
        exit 1
esac

exit 0
